package com.hr.builder;

import java.util.List;

import org.springframework.stereotype.Component;

import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.pagelayout.DetailLayout;
import com.espirit.eap.pagelayout.Element;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.FunctionButton;
import com.espirit.eap.pagelayout.GridSection;
import com.espirit.eap.pagelayout.ListLayout;
import com.espirit.eap.pagelayout.Model;
import com.espirit.eap.pagelayout.PageReference;
import com.hr.Schedual;
import com.hr.SchedualItem;

@Component
public class SchedualBuilder extends ModelBuilderSupport {
	static final String DEPARTMENT = "department";
	static final String CREATED = "created";
	static final String SCHEDUALITEMS = "schedualItems";
	static final String MONTH = "acctTerm";
	static final String WEEK = "week";
	static final String DEPTIDS = "deptIds";
	
	FunctionButton generateSchedual = null;
	
	@Override
	public boolean init() {
		return super.setClass(Schedual.class, "Schedual", "排班");
	}	

	@Override
	public void buildFields() {
		super.buildFields();
		addModelField(DEPARTMENT, "Department", "部门");
		addModelField(DEPTIDS, "Department", "部门");
		addModelField(MONTH, "Month", "月");
		addModelField(WEEK, "Week", "周");
		addModelField(CREATED, "Created", "创建日期");
	}
	
	@Override
	public void prepareLayoutElements() {
		generateSchedual = createFunctionButton("_generateSchedual", "Generate Bank Collection", "生成排班",this.clazz,"_generateSchedual",false,null,null);
		super.prepareLayoutElements();
	}
	
	@Override
	protected void setDefaultLayoutButtons() {
		addButton(generateSchedual);
		addButton(FunctionButton.DELETE);
		addButton(FunctionButton.EXPORT);
	}
	
	@Override
	protected void addExtraListLayoutButtons() {
//		addButton(generateSchedual);
	}
	
	@Override
	protected void addSearchFields() {
		super.addSearchField(convertPropertyToField( DEPTIDS, MONTH));
	}
	
	@Override
	protected void addListFields() {
		addListField(convertPropertyToField(DEPARTMENT, MONTH, WEEK, CREATED));
	}
	
	@Override
	protected void addDetailFields() {
		addDetailField(convertPropertyToField( DEPARTMENT, MONTH, WEEK));
	}
	
	
	@Override
	protected void afterBuilderLayout() {
		DetailLayout layout = getDefaultDetailLayout();

		FlatSection fsResult =  layout.createFlatSection();
		fsResult.setNamePrefix("model");
		
		Model schedualItemView = getModel(SchedualItem.class);
		ListLayout listLayout = schedualItemView.getDefaultListLayout();
		
		List<Element> children = elementService.createSectionElement(fsResult, listLayout.getSortChildren());
		for (Element child : children) {
			if (child instanceof GridSection) {
				GridSection gs = (GridSection)child;
				gs.setReadOnly(false);
				gs.setTagName("datagrid");
				gs.setNamePrefix("schedualItems");		
			}
		}
		
		if (model.getDefaultDetailLayout() != null){
			FlatSection lastSection = (FlatSection) model.getDefaultDetailLayout().getLastSection();
			PageReference eventReference = new PageReference();
			eventReference.setUrl("/pages/espirit/eap/manager/schedual.ftl");
			elementService.createSectionElement(lastSection, eventReference);
			
		}
		
		
		DetailLayout dl = createDetailLayout(model, "generateSchedualLayout", false, convertPropertyToField(MONTH, DEPTIDS));
		
		bindButtonToLayout(generateSchedual, dl);
		
		super.afterBuilderLayout();
	}
	
	@Override
	protected void gatherExproperties() {
		exProperties.put(REQUIRED, new String[]{DEPARTMENT});
	}
}
